/**
 * 
 */
package com.zznode.client.wlanpolling.service.net;

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.util.Log;

import com.zznode.client.wlanpolling.EnumRunMode;
import com.zznode.client.wlanpolling.utils.WLanPollingUtils;

/**
 * @author Bean
 *
 */
public class HttpGetExecutor extends HttpExecutor {
	
	public HttpGetExecutor(Context context) {
		super(context);
	}
	
	@Override
	public byte[] onSyncExecute() throws HttpException {
		if(url == null)
			throw new HttpException("URL地址为空");
		
		if(WLanPollingUtils.RUN_MODE == EnumRunMode.DEBUG) {
			Log.d(HttpGetExecutor.class.getName(), url);
		}
		
		HttpGet httpGet = new HttpGet(addParamsToUrl(url));
		
		try {
			HttpResponse response = new DefaultHttpClient().execute(httpGet);
			if(response.getStatusLine().getStatusCode() == 200){
				byte[] bytes = EntityUtils.toByteArray(response.getEntity());
				if(WLanPollingUtils.RUN_MODE == EnumRunMode.DEBUG) {
					Log.d(HttpGetExecutor.class.getCanonicalName(), new String(bytes, WLanPollingUtils.DEFAULT_CHARSET));
				}
				return bytes;
			} else {
				if(WLanPollingUtils.RUN_MODE == EnumRunMode.DEBUG) {
					Log.d(HttpGetExecutor.class.getCanonicalName(), EntityUtils.toString(response.getEntity()));
				}
				throw new HttpException("提交请求失败, StatusCode=" + response.getStatusLine().getStatusCode());
			}
		} catch (IOException e) {
			throw new HttpException(e.getMessage());
		}
	}

	@Override
	public void onAsyncExecute(HttpCallback callback, int tag) {
		//使用线程发起Http请求
		ExecuteRunable runable = new ExecuteRunable();
		runable.setCallback(callback);
		runable.setExecutore(this);
		runable.setTag(tag);
		
		Thread thread = new Thread(runable);
		thread.start();
	}
	
	private String addParamsToUrl(String url) {
		StringBuilder sb = new StringBuilder();
		
		if((url.contains("?") && url.endsWith("?")) ) {
			sb.append(url);
		} else if(!url.contains("?")) {
			sb.append(url).append("?");
		} else if(url.contains("?") && !url.endsWith("?")) {
			sb.append(url).append("&");
		}
		
		int i = 0;
		for (HttpParam param : params) {
			if(i != 0) {
				sb.append("&");
			}
			sb.append(param.getName()).append("=");
			sb.append(URLEncoder.encode(param.getValue()));
			i++;
		}
		
		Log.d(HttpGetExecutor.class.getCanonicalName(), sb.toString());
		
		return sb.toString();
	}
	
	private class ExecuteRunable implements Runnable {
		private HttpGetExecutor executor;
		private	HttpCallback callback;
		private int tag;
		
		public void setExecutore(HttpGetExecutor executor) {
			this.executor = executor;
		}

		public void setCallback(HttpCallback callback) {
			this.callback = callback;
		}
		

		public void setTag(int tag) {
			this.tag = tag;
		}

		@Override
		public void run() {
			if(executor == null)
				return;
			
			byte[] bytes = new byte[0];
			HttpException exp1 = null;
			
			try {
				bytes = executor.syncExecute();
			} catch(HttpException exp) {
				exp1 = exp;
			}
			
			if(callback != null)
				callback.onReceiveData(bytes, exp1, tag);
		}
	}
}
